package com.ishell.generate.pattern.strategy;

import com.ishell.generate.TableDesc;
import com.ishell.generate.util.JdbcUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: crazier.huang
 * @project: generate
 * @date: 2018/7/12
 * @desc:
 */
public class GenTableMysqStrategy implements GenTableStrategy {
    private final static Logger logger = LoggerFactory.getLogger(GenTableMysqStrategy.class);



    // 获取表
    String tableSql = "select TABLE_NAME,TABLE_COMMENT from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = '"+dbName+"';";


    @Override
    public List<TableDesc> genTable() {
        List<TableDesc> tableDescList = new ArrayList<>();

        try {
            JdbcUtil jdbcUtil = new JdbcUtil();
            jdbcUtil.getConnection();
            List<Map<String,Object>> tlist = jdbcUtil.findResult(tableSql,null);

            for(Map<String,Object> map : tlist){
                TableDesc tableDesc = new TableDesc(map);
                if(!(null==excludeList) && excludeList.contains(tableDesc.getName())){
                    continue;
                }
                if(!(null==includeList) && !(includeList.contains(tableDesc.getName()))){
                    continue;
                }

                tableDescList.add(tableDesc);
            }

        } catch (SQLException e) {
            logger.error("执行脚本报错",e);
            return null;

        }



        return tableDescList;
    }
}
